package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class UserGroupPeer extends BasePeer{
	public static final String tableName = "_User_Group";
	public static final String USERID = "userId";
	public static final String GROUPID = "groupId";
	public static final String ROLE = "role";
	public static final String NICKNAME = "nickName";
	public static final String SIGNINGUPPERMISSION = "signingUpPermission";
	public static final String POSTPERMISSION = "postPermission";
	public static final String UPLOADPERMISSION = "uploadPermission";
	public static final String DOWNLOADPERMISSION = "downloadPermission";
	public static final String CENT = "cent";
	public static final String EXPRIENCE = "exprience";
	public static final String REPLIES = "replies";
	public static final String POSTCOUNTS = "postCounts";
	public static final String ICON = "icon";
	public static final String CONTACTEMAIL = "contactEmail";
	public static final String CONTACTPHONE = "contactPhone";
	public static final String CONTACTIM = "contactIM";
	public static final String CONTACTFUTHER = "contactFuther";
	public static final String ONLINETIME = "onlineTime";
	public static final String LASTVISITE = "lastVisite";
	public static final String LEVEL = "level";
	public static final String PRESTIGE = "prestige";
	public static final String SIGNATURE = "signature";
	public static final String UPDATEAMOUNT = "updateAmount";
	public static final String DOWNLOADAMOUNT = "downloadAmount";
	public static final String SEX = "sex";
	public static final String STATE = "state";
	public static final String[] columnNames=new String[]{"userId","groupId","role","nickName","signingUpPermission","postPermission","uploadPermission","downloadPermission","cent","exprience","replies","postCounts","icon","contactEmail","contactPhone","contactIM","contactFuther","onlineTime","lastVisite","level","prestige","signature","updateAmount","downloadAmount","sex","state"};
	public static UserGroup getUserGroupByPKs(Object groupId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserGroupPeer.tableName,UserGroupPeer.columnNames," TRUE  AND `groupId`=?  AND `userId`=? ",new Object[]{groupId,userId,},0, 1,conn);
			UserGroup userGroup = new UserGroup();
		fill(rs,userGroup);
		return userGroup;
	}
	public static UserGroup getUserGroupReferenceColumnsOnlyByPKs(Object groupId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserGroupPeer.tableName,new String[]{"groupId","userId",}," TRUE AND `groupId`=?AND `userId`=?",new Object[]{groupId,userId,}, 0,  1,conn);
			UserGroup userGroup = new UserGroup();
		fill(rs,userGroup);
		return userGroup;
	}
	public static UserGroup getUserGroupByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserGroupPeer.tableName,UserGroupPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			UserGroup userGroup = new UserGroup();
		fill(rs,userGroup);
		return userGroup;
	}
	public static UserGroup getUserGroupByGroupId(long groupId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(UserGroupPeer.tableName,UserGroupPeer.columnNames,"`groupId`=?",new Object[]{groupId},0, 1,conn);
			UserGroup userGroup = new UserGroup();
		fill(rs,userGroup);
		return userGroup;
	}
	public static void fill(ResultSet rs, UserGroup userGroup) throws SQLException {
		if(rs.next()){
			try{userGroup.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userGroup.setGroupId(rs.getLong("groupId"));}catch(Exception ex){}
			try{userGroup.setRole(rs.getInt("role"));}catch(Exception ex){}
			try{userGroup.setNickName(rs.getString("nickName"));}catch(Exception ex){}
			try{userGroup.setSigningUpPermission(rs.getInt("signingUpPermission"));}catch(Exception ex){}
			try{userGroup.setPostPermission(rs.getInt("postPermission"));}catch(Exception ex){}
			try{userGroup.setUploadPermission(rs.getInt("uploadPermission"));}catch(Exception ex){}
			try{userGroup.setDownloadPermission(rs.getInt("downloadPermission"));}catch(Exception ex){}
			try{userGroup.setCent(rs.getLong("cent"));}catch(Exception ex){}
			try{userGroup.setExprience(rs.getLong("exprience"));}catch(Exception ex){}
			try{userGroup.setReplies(rs.getLong("replies"));}catch(Exception ex){}
			try{userGroup.setPostCounts(rs.getLong("postCounts"));}catch(Exception ex){}
			try{userGroup.setIcon(rs.getLong("icon"));}catch(Exception ex){}
			try{userGroup.setContactEmail(rs.getString("contactEmail"));}catch(Exception ex){}
			try{userGroup.setContactPhone(rs.getString("contactPhone"));}catch(Exception ex){}
			try{userGroup.setContactIM(rs.getString("contactIM"));}catch(Exception ex){}
			try{userGroup.setContactFuther(rs.getString("contactFuther"));}catch(Exception ex){}
			try{userGroup.setOnlineTime(rs.getTimestamp("onlineTime"));}catch(Exception ex){}
			try{userGroup.setLastVisite(rs.getTimestamp("lastVisite"));}catch(Exception ex){}
			try{userGroup.setLevel(rs.getInt("level"));}catch(Exception ex){}
			try{userGroup.setPrestige(rs.getInt("prestige"));}catch(Exception ex){}
			try{userGroup.setSignature(rs.getString("signature"));}catch(Exception ex){}
			try{userGroup.setUpdateAmount(rs.getLong("updateAmount"));}catch(Exception ex){}
			try{userGroup.setDownloadAmount(rs.getLong("downloadAmount"));}catch(Exception ex){}
			try{userGroup.setSex(rs.getInt("sex"));}catch(Exception ex){}
			try{userGroup.setState(rs.getInt("state"));}catch(Exception ex){}
			userGroup.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<UserGroup> userGroupCollection) throws SQLException {
		while(rs.next()){
			UserGroup userGroup=new UserGroup();
			try{userGroup.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{userGroup.setGroupId(rs.getLong("groupId"));}catch(Exception ex){}
			try{userGroup.setRole(rs.getInt("role"));}catch(Exception ex){}
			try{userGroup.setNickName(rs.getString("nickName"));}catch(Exception ex){}
			try{userGroup.setSigningUpPermission(rs.getInt("signingUpPermission"));}catch(Exception ex){}
			try{userGroup.setPostPermission(rs.getInt("postPermission"));}catch(Exception ex){}
			try{userGroup.setUploadPermission(rs.getInt("uploadPermission"));}catch(Exception ex){}
			try{userGroup.setDownloadPermission(rs.getInt("downloadPermission"));}catch(Exception ex){}
			try{userGroup.setCent(rs.getLong("cent"));}catch(Exception ex){}
			try{userGroup.setExprience(rs.getLong("exprience"));}catch(Exception ex){}
			try{userGroup.setReplies(rs.getLong("replies"));}catch(Exception ex){}
			try{userGroup.setPostCounts(rs.getLong("postCounts"));}catch(Exception ex){}
			try{userGroup.setIcon(rs.getLong("icon"));}catch(Exception ex){}
			try{userGroup.setContactEmail(rs.getString("contactEmail"));}catch(Exception ex){}
			try{userGroup.setContactPhone(rs.getString("contactPhone"));}catch(Exception ex){}
			try{userGroup.setContactIM(rs.getString("contactIM"));}catch(Exception ex){}
			try{userGroup.setContactFuther(rs.getString("contactFuther"));}catch(Exception ex){}
			try{userGroup.setOnlineTime(rs.getTimestamp("onlineTime"));}catch(Exception ex){}
			try{userGroup.setLastVisite(rs.getTimestamp("lastVisite"));}catch(Exception ex){}
			try{userGroup.setLevel(rs.getInt("level"));}catch(Exception ex){}
			try{userGroup.setPrestige(rs.getInt("prestige"));}catch(Exception ex){}
			try{userGroup.setSignature(rs.getString("signature"));}catch(Exception ex){}
			try{userGroup.setUpdateAmount(rs.getLong("updateAmount"));}catch(Exception ex){}
			try{userGroup.setDownloadAmount(rs.getLong("downloadAmount"));}catch(Exception ex){}
			try{userGroup.setSex(rs.getInt("sex"));}catch(Exception ex){}
			try{userGroup.setState(rs.getInt("state"));}catch(Exception ex){}
			userGroup.resetModified();
			userGroupCollection.add(userGroup);
		}
	}
	public static boolean update(UserGroup userGroup, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < UserGroupPeer.columnNames.length; i++) {
			if (((UserGroupBase)userGroup).modified[i]) {
				columns.add(UserGroupPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `_User_Group` SET ");
		sql.append("WHERE TRUE AND `groupId` = ? AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,userGroup.get(column));
		}
		pstmt.setObject(columns.size()+1+0,userGroup.get("groupId"));
		pstmt.setObject(columns.size()+1+1,userGroup.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(UserGroup userGroup, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (userGroup.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `_User_Group` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (userGroup.get(column) != null) {
				pstmt.setObject(i + 1 + j, userGroup.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<UserGroup> getUserGroupCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<UserGroup> collection=new Vector<UserGroup>();
		fill(rs,collection);
		return collection;
	}
}
